package org.dingfugui.triple.context.producer;

import org.dingfugui.triple.bean.Producer;
import org.dingfugui.triple.context.Event;
import org.dingfugui.triple.context.ProducerFactory;

/**
 * Created by fugui.ding on 2015/4/2.
 */
public class DefaultExecuteHandler implements ExecuteHandler {

    ProducerFactory producerFactory;

    public DefaultExecuteHandler(ProducerFactory producerFactory) {
        this.producerFactory = producerFactory;
    }

    @Override
    public Event handle(Event event) {
        Object obj = invoke(event.getName(), event.getMethod(), event.getTypes(), event.getParams());
        event.setResult(obj);
        return event;
    }

    protected Object invoke(String serivce, String method, Class[] types, Object[] params) {
        Producer producer = producerFactory.getProducer(serivce);
        Object result = producer.invoke(method, types, params);
        return result;
    }

}
